    //初始化界面
    $(function() {
        var listTitle = ""; //单据标题
        var supliperList = null;
        // var listType = "销售订单列表"; //类型
        var listTypeEn = ""; //英文类型
        //getType();
        initTableData();
        ininPager();
        initSupplier();
        bindEvent();
    });
    //获取财务员
    function initSystemData_person(){
        var type = "客户";
        $.ajax({
            type:"get",
            url: "/supplier/findBySelect_cus",
            data: {
                type: type
            },
            //设置为同步
            async:false,
            dataType: "json",
            success: function (res) {
                if(res && res.code === 200) {
                    personList = res.data.personList;
                }
            }
        });
    }
    //初始化单位信息
    function initSupplier(){
        $('#OrganId').combobox({
            url: '/supplier/findBySelect_cus',
            valueField:'id',
            textField:'supplier',
            formatter: function(row){
                var opts = $(this).combobox('options');
                supliperList = row;
                if(row[opts.textField]!=="非会员") {
                    return row[opts.textField];
                }
            }
        });
        $('#searchKhxx').combobox({
            url: '/supplier/findBySelect_cus',
            valueField:'id',
            textField:'supplier',
            formatter: function(row){
                var opts = $(this).combobox('options');
                if(row[opts.textField]!=="非会员") {
                    return row[opts.textField];
                }
            }
        });
    }
    //获取财务员
    function initSelectInfo_person(){
        var options = "";

        if(supliperList !=null)
        {
            for(var i = 0 ;i < supliperList.length;i++)
            {
                var person = supliperList[i];
                if(0 == i)
                {
                    personID = person.id;
                }
                if(person.type=="客户")
                {
                    options += '<option value="' + person.id + '">' + person.name + '</option>';
                }
            }
            $("#HandsPersonId").empty().append(options);
        }
    }

    //初始化表格数据
    function initTableData() {
        //改变宽度和高度
        $("#searchPanel").panel({width:webW-2});
        $("#tablePanel").panel({width:webW-2});
        $('#tableData').datagrid({
            //title:'单位列表',
            //iconCls:'icon-save',
            //width:700,
            height:heightInfo,
            nowrap: false,
            rownumbers: false,
            //动画效果
            animate:false,
            //选中单行
            singleSelect : true,
            collapsible:false,
            selectOnCheck:false,
            //宽度不够，故不采用自适应
            fitColumns:true,
            //单击行是否选中
            checkOnSelect : false,
            //交替出现背景
            striped : true,
            pagination: true,
            //自动截取数据
            //nowrap : true,
            //loadFilter: pagerFilter,
            pageSize: initPageSize,
            pageList: initPageNum,
            columns:[[
                { field: 'id',width:35,align:"center",checkbox:true},
                { title: '操作',field: 'op',align:"center",width:60,
                    formatter:function(value,rec,index) {
                        var str = '';
                        str += '<img title="编辑" src="/js/easyui/themes/icons/pencil.png" style="cursor: pointer;" onclick="editOrder(\'' + index + '\');"/>&nbsp;&nbsp;&nbsp;';
                        if(isShowOpFun()) {
                            str += '<img title="删除" src="/js/easyui/themes/icons/edit_remove.png" style="cursor: pointer;" onclick="deleteOrder(\'' + rec.id + '\');"/>';
                        }
                        return str;
                    }
                },
                // { title: '工序操作',field: 'end',align:"center",width:60,
                //     formatter:function(value,rec,index) {
                //         var str = '';
                //         str += '<img title="结束" src="/js/easyui/themes/icons/end.png" style="cursor: pointer;" onclick="endOrder(\'' + index + '\');"/>&nbsp;&nbsp;&nbsp;';
                //         return str;
                //     }
                // },
                { title: '客户', field: 'organName',width:100,align:"center"},
                { title: '规格', field: 'guige',width:100,align:"center"},
                { title: '材质', field: 'caizhi',width:100,align:"center"},
                { title: '数量',field: 'number',width:70,align:"center"},
                { title: '油沟',field: 'yougou',width:70,align:"center"},
                { title: '重量',field: 'weight',width:70,align:"center"},
                { title: '订货日期', field: 'dhrq',width:200,align:"center"},
                { title: '预计交货日期', field: 'yjjhrq',width:200,align:"center"},
                { title: '炉号', field: 'shaojieluhao',width:60,align:"center"},
                { title: '状态',field: 'status',width:70,align:"center",formatter:function(value){
                    if(value == 0){
                        return "<span style='color:green'>未生产</span>";
                    }else if (value == 1){
                        return "<span style='color:red'>生产中</span>";
                    }else if (value == 2){
                        return "<span style='color:red'>生产结束</span>";
                    }

                }}
            ]],
            toolbar:[
                {
                    id:'addSupplier',
                    text:'增加',
                    iconCls:'icon-add',
                    handler:function() {
                        addOrder();
                    }
                },'-',
                {
                    id:'deleteSupplier',
                    text:'删除',
                    iconCls:'icon-remove',
                    handler:function() {
                        batDeleteOrder();
                    }
                },/**'-',
                {
                    id:'setEnable',
                    text:'启用',
                    iconCls:'icon-ok',
                    handler:function() {
                        setEnableFun();
                    }
                },'-',
                {
                    id:'setDisEnable',
                    text:'禁用',
                    iconCls:'icon-no',
                    handler:function() {
                        setDisEnableFun();
                    }
                },*/
                '-',
                {
                    id:'setInput',
                    text:'导入',
                    iconCls:'icon-excel',
                    handler:function() {
                        setInputFun();
                    }
                },'-',
                {
                    id:'setOutput',
                    text:'导出',
                    iconCls:'icon-excel',
                    handler:function() {
                        setOutputFun();
                    }
                }
            ],
            onLoadError:function() {
                $.messager.alert('页面加载提示','页面加载异常，请稍后再试！','error');
                return;
            }
        });
        dgResize();
        toolbarStatus();
        showOrderDetails(1,initPageSize);
    }


    //分页信息处理
    function ininPager() {
        try {
            var opts = $("#tableData").datagrid('options');
            var pager = $("#tableData").datagrid('getPager');
            pager.pagination({
                onSelectPage:function(pageNum, pageSize)
                {
                    opts.pageNumber = pageNum;
                    opts.pageSize = pageSize;
                    pager.pagination('refresh', {
                        pageNumber:pageNum,
                        pageSize:pageSize
                    });
                    showOrderDetails(pageNum,pageSize);
                }
            });
        }
        catch (e) {
            $.messager.alert('异常处理提示',"分页信息异常 :  " + e.name + ": " + e.message,'error');
        }
    }

    //删除信息
    function deleteOrder(id) {
        $.messager.confirm('删除确认','确定要删除此条信息吗？',function(r) {
            if (r) {
                $.ajax({
                    type:"post",
                    url: "/scorder/batchDeleteOrderByIds",
                    dataType: "json",
                    data: ({
                        ids : id
                    }),
                    success: function (res) {
                        if(res && res.code == 200) {
                            $("#searchBtn").click();
                        } else {
                            if(res && res.code == 601){
                                var jsondata={};
                                jsondata.ids=id;
                                jsondata.deleteType='2';
                                var type='single';
                                batDeleteSupplierForceConfirm(res,"/supplier/batchDeleteSupplierByIds",jsondata,type);
                            }else if(res && res.code == 600){
                                $.messager.alert('删除提示', res.msg, 'error');
                            }else{
                                $.messager.alert('删除提示', '删除信息失败，请稍后再试！', 'error');
                            }
                        }
                    },
                    //此处添加错误处理
                    error:function() {
                        $.messager.alert('删除提示','删除信息异常，请稍后再试！','error');
                        return;
                    }
                });
            }
        });
    }

    //批量删除单位
    function batDeleteOrder() {
        var row = $('#tableData').datagrid('getChecked');
        if(row.length == 0) {
            $.messager.alert('删除提示','没有记录被选中！','info');
            return;
        }
        if(row.length > 0) {
            $.messager.confirm('删除确认','确定要删除选中的' + row.length + '条信息吗？',function(r) {
                if (r) {
                    var ids = "";
                    for(var i = 0;i < row.length; i ++) {
                        if(i == row.length-1)
                        {
                            ids += row[i].id;
                            break;
                        }
                        ids += row[i].id + ",";
                    }
                    $.ajax({
                        type:"post",
                        url: "/scorder/batchDeleteOrderByIds",
                        dataType: "json",
                        async :  false,
                        data: ({
                            ids : ids
                        }),
                        success: function (res) {
                            if(res && res.code === 200) {
                                $("#searchBtn").click();
                                $(":checkbox").attr("checked", false);
                            } else {
                                if(res && res.code == 601){
                                    var jsondata={};
                                    jsondata.ids=ids;
                                    jsondata.deleteType='2';
                                    var type='batch';
                                    batDeleteSupplierForceConfirm(res,"/supplier/batchDeleteSupplierByIds",jsondata,type);
                                }else if(res && res.code == 600){
                                    $.messager.alert('删除提示', res.msg, 'error');
                                }else{
                                    $.messager.alert('删除提示', '删除信息失败，请稍后再试！', 'error');
                                }
                            }
                        },
                        //此处添加错误处理
                        error:function() {
                            $.messager.alert('删除提示','删除信息异常，请稍后再试！','error');
                            return;
                        }
                    });
                }
            });
        }
    }
    /**
     * 确认强制删除
     * */
    function batDeleteSupplierForceConfirm(res,url,jsondata) {
        $.messager.confirm('删除确认', res.msg, function (r) {
            if (r) {
                $.ajax({
                    type: "post",
                    url: url,
                    dataType: "json",
                    data: (jsondata),
                    success: function (res) {
                        if(res && res.code == 200) {
                            $("#searchBtn").click();
                            if(type=='batch'){
                                $(":checkbox").attr("checked", false);
                            }
                        }else if(res && res.code == 600){
                            $.messager.alert('删除提示', res.msg, 'error');
                        }else {
                            $.messager.alert('删除提示', '删除信息失败，请稍后再试！', 'error');
                        }
                    },
                    //此处添加错误处理
                    error: function () {
                        $.messager.alert('删除提示', '删除信息失败，请稍后再试！', 'error');
                        return;
                    }
                });
            }
        });
    }

    //批量启用
    function setEnableFun() {
        var row = $('#tableData').datagrid('getChecked');
        if(row.length == 0) {
            $.messager.alert('启用提示','没有记录被选中！','info');
            return;
        }
        if(row.length > 0) {
            $.messager.confirm('启用确认','确定要启用选中的' + row.length + '条信息吗？',function(r) {
                if (r) {
                    var ids = "";
                    for(var i = 0;i < row.length; i ++) {
                        if(i == row.length-1) {
                            ids += row[i].id;
                            break;
                        }
                        ids += row[i].id + ",";
                    }
                    $.ajax({
                        type:"post",
                        url: "/supplier/batchSetEnable",
                        dataType: "json",
                        async :  false,
                        data: ({
                            enabled: true,
                            orderIDs : ids
                        }),
                        success: function (res) {
                            if(res && res.code === 200) {
                                $("#searchBtn").click();
                                $(":checkbox").attr("checked", false);
                            } else {
                                $.messager.alert('启用提示', '启用信息失败，请稍后再试！', 'error');
                            }
                        },
                        //此处添加错误处理
                        error:function() {
                            $.messager.alert('启用提示','启用信息异常，请稍后再试！','error');
                            return;
                        }
                    });
                }
            });
        }
    }

    //批量禁用
    function setDisEnableFun() {
        var row = $('#tableData').datagrid('getChecked');
        if(row.length == 0) {
            $.messager.alert('禁用提示','没有记录被选中！','info');
            return;
        }
        if(row.length > 0) {
            $.messager.confirm('禁用确认','确定要禁用选中的' + row.length + '条信息吗？',function(r) {
                if (r) {
                    var ids = "";
                    for(var i = 0;i < row.length; i ++) {
                        if(i == row.length-1) {
                            ids += row[i].id;
                            break;
                        }
                        ids += row[i].id + ",";
                    }
                    $.ajax({
                        type:"post",
                        url: "/supplier/batchSetEnable",
                        dataType: "json",
                        async :  false,
                        data: ({
                            enabled: false,
                            orderIDs : ids
                        }),
                        success: function (res) {
                            if(res && res.code === 200) {
                                $("#searchBtn").click();
                                $(":checkbox").attr("checked", false);
                            } else {
                                $.messager.alert('禁用提示', '禁用信息失败，请稍后再试！', 'error');
                            }
                        },
                        //此处添加错误处理
                        error:function() {
                            $.messager.alert('禁用提示','禁用信息异常，请稍后再试！','error');
                            return;
                        }
                    });
                }
            });
        }
    }

    //导出数据
    function setOutputFun(){
        var khxx = $.trim($("#searchKhxx").val());
        var guige = $.trim($("#searchGuige").val());
        var beginTime = $.trim($("#searchBeginTime").val());
        var endTime = $.trim($("#searchEndTime").val());
        var beizhu = $.trim($("#searchDesc").val());
        window.location.href = "/scorder/exportExcel?browserType=" + getOs()
            + "&khxx=" + khxx + "&beizhu=" + beizhu + "&guige=" + guige + "&beginTime=" + beginTime + "&endTime=" + endTime+ "&type=0&status=&ddNum=";
    }

    //增加单位
    var url;
    var scorderID = 0;
    //保存编辑前的名称
    var oldOrder = "";

    function addOrder() {
        if(checkPower()){
            return;
        }
        $('#scOrderDlg').dialog('open').dialog('setTitle','<img src="/js/easyui/themes/icons/edit_add.png"/>&nbsp;增加生产订单列表信息');
        $(".window-mask").css({ width: webW ,height: webH});
        //$("#ddNum").focus();
        $('#scOrderFM').form('clear');
        oldOrder = "";
        scorderID = 0;
        url = '/scorder/add';
    }
    function endOrder(index) {

    }
    function bindEvent(){
        //导入excel对话框
        $('#importExcelDlg').dialog({
            width: 400,
            closed: true,
            cache: false,
            modal: true,
            collapsible:false,
            closable: true,
            buttons:'#dlg-buttons5'
        });
        //保存信息
        $("#saveScOrder").off("click").on("click", function () {
            if(validateForm("scOrderFM")) {
                return;
            }
            // var reg = /^([0-9])+$/;
            // var phonenum = $.trim($("#phonenum").val());
            // if(phonenum.length>0 && !reg.test(phonenum)) {
            //     $.messager.alert('提示','电话号码只能是数字','info');
            //     $("#phonenum").val("").focus();
            //     return;
            // }
            var obj = $("#scOrderFM").serializeObject();
            $.ajax({
                url: url,
                type:"post",
                dataType: "json",
                data:{
                    info: JSON.stringify(obj)
                },
                success: function(res) {
                    if(res && res.code === 200) {
                        $('#scOrderDlg').dialog('close');
                        //加载完以后重新初始化
                        var opts = $("#tableData").datagrid('options');
                        showOrderDetails(opts.pageNumber, opts.pageSize);
                    }
                }
            });
        });

        //初始化键盘enter事件
        $(document).keydown(function(event) {
            //兼容 IE和firefox 事件
            var e = window.event || event;
            var k = e.keyCode||e.which||e.charCode;
            //兼容 IE,firefox 兼容
            var obj = e.srcElement ? e.srcElement : e.target;
            //绑定键盘事件为 id是指定的输入框才可以触发键盘事件 13键盘事件
            if(k == "13"&&(obj.id=="supplier" || obj.id=="contacts"|| obj.id=="phonenum"
                || obj.id=="email" || obj.id=="description" ))
            {
                $("#saveOrder").click();
            }

            //搜索按钮添加快捷键
            if(k == "13"&&(obj.id=="searchSupplier" || obj.id=="searchContacts"|| obj.id=="searchPhonenum"
                || obj.id=="searchEmail" || obj.id=="searchDesc" ))
            {
                $("#searchBtn").click();
            }
        });

        //搜索处理
        $("#searchBtn").unbind().bind({
            click:function() {
                showOrderDetails(1,initPageSize);
                var opts = $("#tableData").datagrid('options');
                var pager = $("#tableData").datagrid('getPager');
                opts.pageNumber = 1;
                opts.pageSize = initPageSize;
                pager.pagination('refresh', {
                    pageNumber:1,
                    pageSize:initPageSize
                });
            }
        });

        $("#searchBtn").click();

        //重置按钮
        $("#searchResetBtn").unbind().bind({
            click:function(){
                $("#searchKhxx").textbox("clear");
                $("#searchGuige").textbox("clear");
                $("#searchDesc").textbox("clear");
                $("#searchBeginTime").datebox("clear");
                $("#searchEndTime").datebox("clear");
                //加载完以后重新初始化
                $("#searchBtn").click();
            }
        });

    }

    //编辑信息
    function editOrder(index) {
        var res = $("#tableData").datagrid("getRows")[index];
        var sId = res.id;
        var row = {
            gzdbh : res.gzdbh,
            caizhi : res.caizhi,
            guige : res.guige,
            caizhi : res.caizhi,
            number : res.number,
            dhrq : res.dhrq,
            yjjhrq : res.yjjhrq,
            yougou : res.yougou,
            weight : res.weight,
            yycc : res.yycc,
            zhizheng : res.zhizheng,
            shaojieluhao:res.shaojieluhao,
            jitai:res.jitai,
            id:sId,

        };
        oldOrder = res.order;
        $('#scOrderDlg').dialog('open').dialog('setTitle','<img src="/js/easyui/themes/icons/pencil.png"/>&nbsp;编辑生产订单列表信息');
        $(".window-mask").css({ width: webW ,height: webH});
        if(!isShowOpFun()){
            $("#saveScOrder").hide();
        }
        $('#scOrderFM').form('load',row);
        $('#OrganId').combobox('setValue', res.khxx);
        scorderID = sId;
        //焦点在名称输入框==定焦在输入文字后面
        //$("#supplier").focus().val(res.supplier);
        url = '/scorder/update?id=' + sId;
    }

    function showOrderDetails(pageNo,pageSize) {
        var khxx = $.trim($("#searchKhxx").val());
        var guige = $.trim($("#searchGuige").val());
        var beginTime = $.trim($("#searchBeginTime").val());
        var endTime = $.trim($("#searchEndTime").val());
        var beizhu = $.trim($("#searchDesc").val());
        // beginTime= beginTime.replace("-","/");//替换字符，变成标准格式
        // beginTime= new Date(Date.parse(beginTime));
        // endTime= endTime.replace("-","/");//替换字符，变成标准格式
        // endTime= new Date(Date.parse(endTime));
        // if(beginTime > endTime) {
        //     $.messager.alert('提示','开始时间不能大与结束时间','info');
        //     return;
        // }
        $.ajax({
            type:"get",
            url: "/scorder/list",
            dataType: "json",
            data: ({
                search: JSON.stringify({
                    khxx: khxx,
                    guige: guige,
                    beginTime: beginTime,
                    endTime: endTime,
                    status:""
                }),
                currentPage: pageNo,
                pageSize: pageSize
            }),
            success: function (res) {
                if(res && res.code === 200){
                    if(res.data && res.data.page) {
                        $("#tableData").datagrid('loadData', res.data.page);
                    }
                }
            },
            //此处添加错误处理
            error:function() {
                $.messager.alert('查询提示','查询数据后台异常，请稍后再试！','error');
                return;
            }
        });
    }

